<?php
require_once '../../include/common.inc.php';
include '../../include/PHPExcel/PHPExcel.php';
require_once '../excel.php';
//武汉登录数据
function wh_loginLog($stTime,$endTime,$logintype_sql) {
    global $db,$tablepre,$firstcount,$displaypg;
    $sql="select * from (SELECT uname ,ip ,count(*)   FROM chat_msgs where type ='3' $logintype_sql   and mtime < UNIX_TIMESTAMP('$endTime') and mtime > UNIX_TIMESTAMP('$stTime') 
GROUP BY  uname) t2  ";
    $query=$db->query($sql);

    $resutl = [];
    $count=$db->num_rows($db->query($sql));

    while($row=$db->fetch_row($query)){
        $resutl[]=$row;
    }

    if(!empty($resutl)) {
        return [$resutl,$count];
    } else {
        return false;
    }
}


function formatTime($Time)
{
    return date('m-d_H',strtotime($Time));
}

//查询武汉广州数据
function wh_hq_count($start,$end,$logintype_sql)
{
    $wh_count = wh_loginLog($start,$end,$logintype_sql);
    return [$wh_count[1]];
}

function totalcount($start,$end,$timeword,$logintype_sql){
    $moning_s = $start;
    $moning_e= $end;
    $count = wh_hq_count($moning_s,$moning_e,$logintype_sql);
    $total = $count[0]+$count[1];
    echo "$timeword  总计： $count[0]  </br>";
}


$logintype_sql = '';
if($logintype !== ''){
    $logintype_sql = " and logintype = $logintype " ;
}

switch($act){
    case "wh_export":
        $result = wh_loginLog($start,$end,$logintype_sql);

        $count =  $result[1];
        $result =  $result[0];

        $fileName = formatTime($start)."_".formatTime($end)."_".$count;

        $head_array =array('用户名','ip地址','登录次数');

        $objPHPExcel = Excel::export($result,$head_array ,$fileName,$fileName);
        $objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(15);//调节列宽

        Excel::do_export($objPHPExcel,$fileName);
        break;

    //获取固定文字报表
    case "wh_hq_export":

        $start_day = substr($start,0,10).' ';
        if ($start_day == ' '){
            $start_day = date('Y-m-d').' ';
        }

        totalcount($start_day.'09:00:00',$start_day.'12:00:00','早上',$logintype_sql);
        totalcount($start_day.'13:00:00',$start_day.'16:00:00','下午',$logintype_sql);
        totalcount($start_day.'19:00:00',$start_day.'22:00:00','晚上',$logintype_sql);
        totalcount($start_day.'09:00:00',$start_day.'22:00:00',substr($start_day,0,10),$logintype_sql);
        exit;

        break;

}
?>
<!DOCTYPE HTML>
<html>
<head>
    <title>登录统计</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="../assets/css/dpl-min.css" rel="stylesheet" type="text/css" />
    <link href="../assets/css/bui-min.css" rel="stylesheet" type="text/css" />
    <link href="../assets/css/page-min.css" rel="stylesheet" type="text/css" />
    <!-- 下面的样式，仅是为了显示代码，而不应该在项目中使用-->
    <link href="../assets/css/prettify.css" rel="stylesheet" type="text/css"/>
    <style type="text/css">
        code { padding: 0px 4px; color: #d14; background-color: #f7f7f9; border: 1px solid #e1e1e8; }
    </style>
</head>
<body>
<div class="container" style="min-width:700px;">
    <ul class="breadcrumb">
        <!--    <li class="active">-->
        <!--         <button type="submit"  class="button button-success" id="add_group_bt"><i class="icon icon-plus icon-white"></i> 添加</button>-->
        <!--         &nbsp;&nbsp;</li>-->
    </ul>
    <table  class="table table-bordered table-hover definewidth m10" >

        <form method="post" enctype="application/x-www-form-urlencoded">
            <tr>
                <td bgcolor="#FFFFFF" align="center">  <div id="group" class="bui-form-group">
                        <label>起始日期：</label>
                        <input id="start" name="start" value="" class="calendar calendar-time" type="text"><label> - </label>
                        <input id="end" value="" name="end" class="calendar calendar-time" type="text"></div>&nbsp;
                </td>
                <td class="tableleft">是否软件登录：</td>
                <td><select name="s_logintype" id="s_logintype" style="width:60px;">
                        <option value="">全部</option>
                        <option value="1">是</option>
                        <option value="0">否</option>
                    </select>
                </td>

                <td align="center" valign="middle" bgcolor="#FFFFFF"><button act="wh_export"  class="submit_btn button   button-success"  type="submit"><i class="x-icon icon-ok icon-white"></i>数据导出</button></td>
                <td align="center" valign="middle" bgcolor="#FFFFFF"><button act="wh_hq_export"  class="submit_btn button   button-success"  type="submit"><i class="x-icon icon-ok icon-white"></i>统计报表生成</button></td>
            </tr>

            <input type="hidden" id="act" name="act">
            <input type="hidden" id="logintype" name="logintype">

        </form>

    </table>
    <div class="row">

    </div>
</div>
<script type="text/javascript" src="../assets/js/jquery-1.8.1.min.js"></script>
<script type="text/javascript" src="../assets/js/bui.js"></script>
<script type="text/javascript" src="../assets/js/config.js"></script>
<script type="text/javascript" src="../../upload/swfupload/swfupload.js"></script>
<script>




    BUI.use('bui/overlay',function(Overlay){
        dialog = new Overlay.Dialog({
            title:'用户组权限编辑',
            width:800,
            height:600,
            buttons:[],
            bodyContent:''
        });
    });
    function openRule(id,type){
        dialog.set('bodyContent','<iframe src="group_rule.php?id='+id+'&type='+type+'" scrolling="yes" frameborder="0" height="100%" width="100%"></iframe>');
        dialog.updateContent();
        dialog.show();
    }
    function openGroupUser(id,name){
        top.topManager.openPage({
            id : 'GroupUser'+id,
            href : 'users/users.php?gid='+id,
            title : name+' 成员'
        });
        top.topManager.reloadPage();
    }

    $('.submit_btn').click(function(){
        var act = $(this).attr('act');
        $("#act").val(act);
        $("#logintype").val($('#s_logintype').val());
    });

    $('#s_logintime').change(function(){


    });

    function changeTime (){
        var time =  $('#s_logintime').val();
    }

</script>
<script type="text/javascript">

    BUI.use('bui/calendar',function(Calendar){
        var datepicker = new Calendar.DatePicker({
            trigger:'.calendar-time',
            showTime:true,
            autoRender : true
        });
    });
</script>

</body>
</html>
